package com.ctshk.rpc.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataBusinessDTO;
import com.ctshk.rpc.system.entity.MainDataBusiness;
import com.ctshk.rpc.system.mapper.MainDataBusinessMapper;
import com.ctshk.rpc.system.req.MainDataBusinessListReq;
import com.ctshk.rpc.system.req.MainDataBusinessReq;
import com.ctshk.rpc.system.service.IMainDataBusinessService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 办证中心 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-04-02
 */
@DubboService
public class MainDataBusinessServiceImpl extends ServiceImpl<MainDataBusinessMapper, MainDataBusiness> implements IMainDataBusinessService {

    @Autowired
    private MainDataBusinessMapper businessMapper;

    @Override
    public Result<PageResponse<MainDataBusinessDTO>> list(MainDataBusinessListReq req) {
        QueryWrapper<MainDataBusiness> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MainDataBusiness::getIsDeleted, IsDeletedCode.NO.getCode());

        if (null != req.getStatus()) {
            wrapper.lambda().eq(MainDataBusiness::getStatus, req.getStatus());
        }
        if (CollectionUtils.isNotEmpty(req.getIdList())) {
            wrapper.lambda().in(MainDataBusiness::getId, req.getIdList());
        }
        IPage<MainDataBusiness> pageParam = new Page<>(req.getPageNo(), req.getPageSize());
        IPage<MainDataBusiness> pageResult = businessMapper.selectPage(pageParam, wrapper);
        List<MainDataBusiness> records = pageResult.getRecords();
        Map<Long, MainDataBusiness> map =  records.stream().collect(Collectors.toMap(MainDataBusiness::getId, Function.identity()));
        List<MainDataBusinessDTO> dtoList = EntityUtil.copyList(records, MainDataBusinessDTO.class);
        dtoList.forEach(dto -> {
            MainDataBusiness business = map.get(dto.getId());
            String urlList = business.getUrlList();
            log.error("-----------------"+urlList);
            List<Map<String,String>> picUrlList = (List<Map<String,String>>) JSONArray.parse(urlList);
            log.error("-----------------"+picUrlList);
            dto.setPicUrlList(picUrlList);

        });
        PageResponse<MainDataBusinessDTO> pageResponse = new PageResponse<>(dtoList,
                pageResult.getCurrent() == 1,
                pageResult.getTotal() <= (pageResult.getCurrent() * pageResult.getSize()),
                pageResult.getTotal(), pageResult.getCurrent(),
                pageResult.getSize());
        return Result.success(pageResponse);
    }

    @Override
    public Result<MainDataBusinessDTO> detail(Long id) {
        QueryWrapper<MainDataBusiness> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MainDataBusiness::getId, id);
        wrapper.lambda().eq(MainDataBusiness::getIsDeleted, IsDeletedCode.NO.getCode());
        MainDataBusiness mainDataBusiness = businessMapper.selectOne(wrapper);
        MainDataBusinessDTO dto = EntityUtil.copy(mainDataBusiness, MainDataBusinessDTO.class);
//        List<String> urlList = JSON.parseArray(mainDataBusiness.getUrlList(), String.class);
        List<Map<String,String>> picUrlList = (List<Map<String,String>>) JSONArray.parse(mainDataBusiness.getUrlList());
        dto.setPicUrlList(picUrlList);
        return Result.success(dto);
    }

    @Override
    public Result add(MainDataBusinessReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        MainDataBusiness mainDataBusiness = EntityUtil.copy(req, MainDataBusiness.class);
        mainDataBusiness.setId(IdWorker.getId());
        mainDataBusiness.setUrlList(req.getPicUrlList());
        mainDataBusiness.setCreateId(tokenUser.getId());
        mainDataBusiness.setModifiedId(tokenUser.getId());
        mainDataBusiness.setModifiedName(tokenUser.getUsername());
        mainDataBusiness.setGmtCreate(now);
        mainDataBusiness.setGmtModified(now);
        mainDataBusiness.setIsDeleted(IsDeletedCode.NO.getCode());
        baseMapper.insert(mainDataBusiness);
        return Result.success();
    }

    @Override
    public Result update(MainDataBusinessReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        MainDataBusiness mainDataBusiness = EntityUtil.copy(req, MainDataBusiness.class);
        mainDataBusiness.setUrlList(req.getPicUrlList());
        mainDataBusiness.setModifiedId(tokenUser.getId());
        mainDataBusiness.setModifiedName(tokenUser.getUsername());
        mainDataBusiness.setGmtModified(now);
        baseMapper.updateById(mainDataBusiness);
        return Result.success();
    }

    @Override
    public Result delete(Long id) {
        baseMapper.deleteById(id);
        return Result.success();
    }
}
